package com.cj.io.bio;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

/**
 * ClassName: Client <br/>
 * Function: 客户端 <br/>
 * date: 2018/05/09 11:16 <br/>
 *
 * @author chenj
 * @version 1.0.0
 * @since JDK 1.8
 */
public class Client {

    private static int DEFAULT_SERVER_PORT = 12345;
    private static String DEFAULT_SERVER_IP = "127.0.0.1";

    public static void send(String expression){
        send(DEFAULT_SERVER_PORT,expression);
    }

    public static void send(int port,String expression){
        System.out.println("算术表达式：" + expression);

        try (Socket socket = new Socket(DEFAULT_SERVER_IP, port)) {
            try (BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()))) {
                try (PrintWriter out = new PrintWriter(socket.getOutputStream(), true)) {
                    out.println(expression);
                    System.out.println("结果为：" + in.readLine());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
